<?
	include_once('header.php');

	include_once('sidebar.php');

	$edit = $_GET['edit'];
	$offset = $_GET['offset'];
	$limit = $_GET['limit'];
	$action = $_GET['action'];
	if(!$offset) { $offset = "0"; }
	if(!$limit) { $limit = "20"; }
	$new_post = $_POST['new_post'];
	$delete = $_GET['delete'];
?>

		
		<div id="main">
			
<?
	if($delete) {
?>
			<h2>Delete a Post</h2>
<?
		if(!isset($_POST['delete_check'])) {
			$the_post = drizzo_get_the_post($delete);
			if(drizzo_post_permissions_check()) {
?>
			<p>Are you sure you want to send <? echo $the_post['title']; ?> to the TrashHeap?</p>
			<form action="blog.php?delete=<? echo $delete; ?>" method="post" name="deleter" id="deleter">
				<input type="hidden" name="delete_check" value="1"></input>
				<input type="hidden" name="old_id" value="<? echo $delete; ?>"></input>
				<input type="hidden" name="old_author" value="<? echo $the_post['author']; ?>"></input>
				<input type="hidden" name="old_type" value="<? echo $the_post['type']; ?>"></input>
				<input type="hidden" name="old_status" value="<? echo $the_post['status']; ?>"></input>
				<input type="hidden" name="old_page_parent" value="<? echo $the_post['page_parent']; ?>"></input>
				<input type="hidden" name="old_page_order" value="<? echo $the_post['page_order']; ?>"></input>
				<input type="hidden" name="old_title" value="<? echo htmlentities($the_post['title']); ?>"></input>
				<input type="hidden" name="old_content" value="<? echo htmlentities($the_post['content']); ?>"></input>
				<input type="hidden" name="old_tags" value="<? echo $the_post['tags']; ?>"></input>
				<input type="hidden" name="old_comment_status" value="<? echo $the_post['comment_status']; ?>"></input>
				<input type="hidden" name="old_edited_by" value="<? echo $the_post['edited_by']; ?>"></input>
				<input type="hidden" name="old_edited_date" value="<? echo $the_post['edited_date_raw']; ?>"></input>
				<input type="hidden" name="old_date" value="<? echo $the_post['posted_date_raw']; ?>"></input>
				<input type="hidden" name="old_permalink_name" value="<? echo $the_post['permalink_name']; ?>"></input>
				<input type="hidden" name="old_menu_name" value="<? echo $the_post['menu_name']; ?>"></input>
				<input type="hidden" name="old_lat" value="<? echo $the_post['lat']; ?>"></input>
				<input type="hidden" name="old_lon" value="<? echo $the_post['lon']; ?>"></input>
				<p><input type="button" onclick="history.go(-1)" value="No" class="submit"></input><input type="submit" name="submit" value="Yes" class="submit"></input></p>
<?			
			} else {
				echo "<p class=\"alert\">You do not have permission to delete this post.</p>";
			}
		} elseif(isset($_POST['delete_check'])) {
			$trashheap_query = drizzo_move_to_trashheap();
			if($trashheap_query) {
				$delete_query = mysql_query("DELETE FROM `$drizzo_posts` WHERE `ID`='$delete' LIMIT 1");
				if($delete_query) {
					echo "<p class=\"trash\">" . $_POST['old_title'] . " has been piled on the TrashHeap.</p>";
				} else {
					echo "<p class=\"alert\">" . $_POST['old_title'] . " was copied to the TrashHeap but not removed from the posts table.<br />" . mysql_error() ."</p>";
				}
			} else {
				echo "<p class=\"alert\">There was a problem and " . $_POST['old_title'] . " could not be moved to the TrashHeap.<br />" . mysql_error() . "</p>";
			}
		} else {
		}
	}

	if($action == "publish") { // Insert data and set to publish
		
		 if($_POST) {
			unset($id, $title, $content, $tags, $date, $author, $type, $status, $comment_status, $permalink_name, $edit_id, $old_type);

			$id = $_POST['edit_id'];
			$title = addslashes($_POST['title']);
			$content = str_replace('<br />','',addslashes($_POST['content']));
			$tags = drizzo_parse_the_tags($_POST['tags']);
			$_POST['date_year'] = str_pad($_POST['date_year'], 4, "20", STR_PAD_LEFT);
			$_POST['date_month'] = str_pad($_POST['date_month'], 2, "0", STR_PAD_LEFT);
			$_POST['date_day'] = str_pad($_POST['date_day'], 2, "0", STR_PAD_LEFT);
			$_POST['date_hour'] = str_pad($_POST['date_hour'], 2, "0", STR_PAD_LEFT);
			$_POST['date_min'] = str_pad($_POST['date_min'], 2, "0", STR_PAD_LEFT);
			$_POST['date_secs'] = str_pad($_POST['date_secs'], 2, "0", STR_PAD_LEFT);
			$date = $_POST['date_year'] . "-" . $_POST['date_month'] . "-" . $_POST['date_day'] . " " . $_POST['date_hour'] . ":" . $_POST['date_min'] . ":" . $_POST['date_secs'];
			$date = strtotime($date);
			$author = $_POST['author'];
			$type = $_POST['type'];
			$status = "publish";
			$comment_status = $_POST['comment_status'];
			$permalink_name = $_POST['permalink_name'];
			if($permalink_name == "") { $permalink_name = ereg_replace("[^(A-Za-z0-9)|-]", "", strtolower(preg_replace("/ /", "-", $title))); }
			$edit_id = session_user_id;

			drizzo_db_connect();

			if(!isset($_POST['old_id'])) {
				$insert_sql = @mysql_query("INSERT INTO `$drizzo_posts` SET `title`='$title',`content`='$content',`tags`='$tags',`posted_date`=$date,`author`='$author',`type`='$type',`status`='$status',`comment_status`='$comment_status',`permalink_name`='$permalink_name',`edited_date`=" . time() . ",`edited_by`='$edit_id'");
			} else {
				$insert_sql = @mysql_query("UPDATE `$drizzo_posts` SET `title`='$title',`content`='$content',`tags`='$tags',`posted_date`=$date,`author`='$author',`type`='$type',`status`='$status',`comment_status`='$comment_status',`permalink_name`='$permalink_name',`edited_date`=" . time() . ",`edited_by`='$edit_id' WHERE `ID`='$id'");
			}
			
			if($insert_sql) { // If insert is succesful
				echo "<p class=\"save\">" . $title . " has been saved.</p>";
				if(isset($_POST['old_id'])) {
					drizzo_move_to_warehouse();
				}
			} else { // If insert fails
				echo "<p class=\"alert\">There has been a database error. " . $title . " has not been saved.<br />" . mysql_error() . "</p>";
			}
		} else { // If there is no Post data
			echo "<p class=\"alert\">There is nothing to publish.</p>";
		} // End $_POST
	} // End publish

	if(!isset($edit) && ($action != "continue") && ($action != "new") && !$delete && ($action != "continue_new")) { // Default View for non drafts setup

		drizzo_db_connect();
	
		if(!$action || ($action == "publish")) {
			$prev_offset = ($offset + $limit);
			$next_offset = abs(($offset - $limit));
			$total_count = mysql_query("SELECT * FROM `$drizzo_posts` WHERE `status`!='draft' AND `type` IN ('blog','aside') ORDER BY `posted_date`,`ID` ASC");
			$total_count = mysql_num_rows($total_count);
			if($prev_offset < $total_count) { $show_prev = "true"; }
			if($offset !=0) { $show_next = "true"; }
		
			$the_posts = drizzo_get_the_posts($limit, $offset);
?>
			<h2>Current Posts</h2>
<?
		} // End Default View setup
	
		if($action == "view_drafts") { // View drafts setup
			$prev_offset = ($offset + $limit);
			$next_offset = abs(($offset - $limit));
			$total_count = mysql_query("SELECT * FROM `$drizzo_posts` WHERE `status`='draft' and `type` IN ('blog','aside') ORDER BY `posted_date`,`ID` ASC");
			$total_count = mysql_num_rows($total_count);
			if($prev_offset < $total_count) { $show_prev = "true"; }
			if($offset !=0) { $show_next = "true"; }
		
			$the_posts = drizzo_get_the_posts($limit, $offset, "draft");
?>
			<h2>Draft Posts</h2>
<?
		} // End drafts view setup
	
		if(!$action || ($action == "view_drafts") || ($action == "publish")) { // Default view table of posts
		
			if(($show_prev == "true") || ($show_next == "true")) {  // Previous & Next top navigation
				echo "<p class=\"right\">";
			}
			if($show_prev == "true") {
				echo "<a href=\"blog.php?offset=" . $prev_offset . "\" title=\"Earlier Entries\">&laquo; Earlier Entries</a>";
			}
			if(($show_prev == "true") && ($show_next == "true")) {
				echo " | ";
			}
			if($show_next == "true") {
				echo "<a href=\"blog.php?offset=" . $next_offset . "\" title=\"Later Entries\">Later Entries &raquo;</a>";
			}
			if(($show_prev == "true") || ($show_next == "true")) {
				echo "</p>";
			}
	}
?>			
			<table id="blog_posts">
				<tr id="first">
					<th class="center">ID</th><th>Title</th><th>Tags</th><th>Created</th><th class="center">Comments</th><th class="center">Author</th><th class="center" colspan="3">Action</th>
				</tr>
<?
			if(count($the_posts) > 0) {
				foreach($the_posts as $the_post) {
					extract($the_post);
					if ($alt_count%2) { 
						echo "<tr class=\"alt\">\n";
					} else {
						echo "<tr>\n";
					}
					echo "\t<td class=\"center\">" . $ID . "</td>";
					if($the_post['type'] == 'aside') {
						echo "<td class=\"aside\">" . $title . "</td>";
					} else {
						echo "<td>" . $title . "</td>";
					}
					echo "<td>" . drizzo_post_tags() . "</td><td>" . $posted_date . "</td><td class=\"center\" width=\"1\">" . $comment_count . "</td><td class=\"center\" width=\"1\">" . drizzo_post_author('nice_name') . "</td><td class=\"center\" width=\"1\"><a href=\"" . drizzo_post_permalink() ."\" title=\"View This Posts\"><img src=\"" . $drizzo_admin_images . "view.png\" alt=\"View This Post\" /></a></td><td class=\"center\" width=\"1\">" . drizzo_edit_link_backend() . "</td><td class=\"center\" width=\"1\">" . drizzo_delete_link_backend() . "</td>\n";
					echo "</tr>\n";
					$alt_count++;
				}
			}
						
			if(count($the_posts) == 0) {
				echo "<tr>\n";
				echo "\t<td colspan=\"9\">No Posts were found.</td>\n";
				echo "</tr>\n";
			}
?>					
			</table>

<?
			if(($show_prev == "true") || ($show_next == "true")) { // Bottom Previous & Next
				echo "<p class=\"right\">";
			}
			if($show_prev == "true") {
				echo "<a href=\"blog.php?offset=" . $prev_offset . "\" title=\"Earlier Entries\">&laquo; Earlier Entries</a>";
			}
			if(($show_prev == "true") && ($show_next == "true")) {
				echo " | ";
			}
			if($show_next == "true") {
				echo "<a href=\"blog.php?offset=" . $next_offset . "\" title=\"Later Entries\">Later Entries &raquo;</a>";
			}
			if(($show_prev == "true") || ($show_next == "true")) {
				echo "</p>";
			}
?>
			<br class="clear" />
<?
		} // End Default View

	if(($action == "continue_new") && $_POST) {
		if($_POST) {
			$id = "";
			$title = "";
			$content = "";
			$tags = "";
			$date = "";
			$author = "";
			$type = "";
			$status = "";
			$comment_status = "";
			$permalink_name = "";
			$edit_id = "";
			$old_type = "";

			$id = $_POST['edit_id'];
			$title = addslashes($_POST['title']);
			$content = str_replace('<br />','',addslashes($_POST['content']));
			$tags = drizzo_parse_the_tags($_POST['tags']);
			$_POST['date_year'] = str_pad($_POST['date_year'], 4, "20", STR_PAD_LEFT);
			$_POST['date_month'] = str_pad($_POST['date_month'], 2, "0", STR_PAD_LEFT);
			$_POST['date_day'] = str_pad($_POST['date_day'], 2, "0", STR_PAD_LEFT);
			$_POST['date_hour'] = str_pad($_POST['date_hour'], 2, "0", STR_PAD_LEFT);
			$_POST['date_min'] = str_pad($_POST['date_min'], 2, "0", STR_PAD_LEFT);
			$_POST['date_secs'] = str_pad($_POST['date_secs'], 2, "0", STR_PAD_LEFT);
			$date = $_POST['date_year'] . "-" . $_POST['date_month'] . "-" . $_POST['date_day'] . " " . $_POST['date_hour'] . ":" . $_POST['date_min'] . ":" . $_POST['date_secs'];
			$date = strtotime($date);
			$author = $_POST['author'];
			$type = $_POST['type'];
			$status = "draft";
			$comment_status = $_POST['comment_status'];
			$permalink_name = $_POST['permalink_name'];
			$permalink_name = ereg_replace("[^(A-Za-z0-9)|-]", "", strtolower(preg_replace("/ /", "-", $permalink_name)));
			if($permalink_name == "") { $permalink_name = ereg_replace("[^(A-Za-z0-9)|-]", "", strtolower(preg_replace("/ /", "-", $title))); }
			$edit_id = session_user_id;
			$old_type = $_POST['old_type'];

			drizzo_db_connect();

			if($_POST['start_time'] == $date) {
				$date = "" . time() . "";
			} else {
				$date = "`" . $date . "`";
			}

			$insert_sql = @mysql_query("INSERT INTO `$drizzo_posts` SET `title`='$title',`content`='$content',`tags`='$tags',`posted_date`='$date',`author`='$author',`type`='$type',`status`='$status',`comment_status`='$comment_status',`permalink_name`='$permalink_name',`edited_date`=" . time() . ",`edited_by`='$edit_id'");
			$id = mysql_insert_id();

			if($insert_sql) {
				$alert = "<p class=\"save\">" . $title . " has been saved. You may continue editing.</p>";
			} else {
				$alert = "<p class=\"alert\">There has been a database error. " . $title . " has not been saved.<br />" . mysql_error() . "</p>";
			}

			echo $alert;

	//		if($old_type != $type) {
	//			$new_location = "Location: http://" . domain_name . admin_folder . $type . ".php?edit=" . $id . "\"";
	//			header($new_location);
	//		}

			$the_post = drizzo_get_the_post($id);
		} else {
			echo "<p class=\"alert\">There is nothing to publish.</p>";
		}

	} // End $action == continue_new
	
	if($edit) {

		if(($action == "continue") && $_POST) {
			if($_POST) {
				unset($id, $title, $content, $tags, $date, $author, $type, $status, $comment_status, $permalink_name, $edit_id, $old_type);

				$id = $_POST['edit_id'];
				$title = addslashes($_POST['title']);
				$content = str_replace('<br />','',addslashes($_POST['content']));
				$tags = drizzo_parse_the_tags($_POST['tags']);
				$_POST['date_year'] = str_pad($_POST['date_year'], 4, "20", STR_PAD_LEFT);
				$_POST['date_month'] = str_pad($_POST['date_month'], 2, "0", STR_PAD_LEFT);
				$_POST['date_day'] = str_pad($_POST['date_day'], 2, "0", STR_PAD_LEFT);
				$_POST['date_hour'] = str_pad($_POST['date_hour'], 2, "0", STR_PAD_LEFT);
				$_POST['date_min'] = str_pad($_POST['date_min'], 2, "0", STR_PAD_LEFT);
				$_POST['date_secs'] = str_pad($_POST['date_secs'], 2, "0", STR_PAD_LEFT);
				$date = $_POST['date_year'] . "-" . $_POST['date_month'] . "-" . $_POST['date_day'] . " " . $_POST['date_hour'] . ":" . $_POST['date_min'] . ":" . $_POST['date_secs'];
				$date = strtotime($date);
				$author = $_POST['author'];
				$type = $_POST['type'];
				$status = $_POST['status'];
				$comment_status = $_POST['comment_status'];
				$permalink_name = $_POST['permalink_name'];
				$permalink_name = ereg_replace("[^(A-Za-z0-9)|-]", "", strtolower(preg_replace("/ /", "-", $permalink_name)));
				if($permalink_name == "") { $permalink_name = ereg_replace("[^(A-Za-z0-9)|-]", "", strtolower(preg_replace("/ /", "-", $title))); }
				$edit_id = session_user_id;
				$old_type = $_POST['old_type'];

				drizzo_db_connect();

				$update_sql = @mysql_query("UPDATE `$drizzo_posts` SET `title`='$title',`content`='$content',`tags`='$tags',`posted_date`='$date',`author`='$author',`type`='$type',`status`='$status',`comment_status`='$comment_status',`permalink_name`='$permalink_name',`edited_date`=" . time() . ",`edited_by`='$edit_id' WHERE `ID`='$id'");

				if($update_sql) { // Give user feedback and move old data to the warehouse
					$alert = "<p class=\"save\">" . $title . " has been saved. You may continue editing.</p>";

					drizzo_move_to_warehouse();

				} else {
					$alert = "<p class=\"alert\">There has been a database error. " . $title . " has not been saved.<br />" . mysql_error() . "</p>";
				}
			} else {
				echo "<p class=\"alert\">No post was being worked on.</p>";
			}
		} // End $action == continue
		

		echo $alert;
		
//		if($old_type != $type) {
//			$new_location = "Location: http://" . domain_name . admin_folder . $type . ".php?edit=" . $id . "\"";
//			header($new_location);
//		}

		$the_post = drizzo_get_the_post($edit);

		if($alert != 0) {
			echo $alert;
			$alert = 0;
		}
	} // End $edit
	
	if($edit || (($action == "continue_new") && $_POST)) {
		if(!drizzo_post_permissions_check()) {
			echo "<p class=\"alert\">You do not have permission to edit this post.</p>";
		} else {
?>
			<form id="editor_form" name="editor" method="post" action="">

			<div id="editor">
				
				<label for="title">Title</label><input name="title" type="text" value="<? echo drizzo_post_title(); ?>"></input><br />

				<label for="content">Content</label><textarea name="content"><? echo drizzo_post_content_backend(); ?></textarea><br />

				<label for="tags">Tags<br /><small>(Separate by commas)</small></label><textarea name="tags" type="text" id="tags_input"><? echo drizzo_post_tags($edit, "comma", "false"); if(drizzo_post_tags($edit)) { echo ", "; }?></textarea><br />
				
				<label for="recent_tags">Recently Used Tags</label><p id="recent_tags"><? echo drizzo_recent_tags(); ?></p>

				<label for="date">Date</label>	<input name="date_month" class="date" type="text" value="<? echo drizzo_post_date('month'); ?>"></input>
												<input name="date_day" class="date" type="text" value="<? echo drizzo_post_date('date'); ?>"></input>
												<input name="date_year" class="date" type="text" value="<? echo drizzo_post_date('year'); ?>"></input>, 
												<input name="date_hour" class="date" type="text" value="<? echo drizzo_post_date('hour_24'); ?>"></input>:
												<input name="date_min" class="date" type="text" value="<? echo drizzo_post_date('minutes'); ?>"></input>:
												<input name="date_secs" class="date" type="text" value="<? echo drizzo_post_date('seconds'); ?>"></input>
												<br />

				<input type="hidden" name="edit_id" value="<? echo $edit; ?>"></input>
				<input type="hidden" name="old_id" value="<? echo $edit; ?>"></input>
				<input type="hidden" name="old_author" value="<? echo $the_post['author']; ?>"></input>
				<input type="hidden" name="old_type" value="<? echo $the_post['type']; ?>"></input>
				<input type="hidden" name="old_status" value="<? echo $the_post['status']; ?>"></input>
				<input type="hidden" name="old_page_parent" value="<? echo $the_post['page_parent']; ?>"></input>
				<input type="hidden" name="old_page_order" value="<? echo $the_post['page_order']; ?>"></input>
				<input type="hidden" name="old_title" value="<? echo htmlentities($the_post['title']); ?>"></input>
				<input type="hidden" name="old_content" value="<? echo htmlentities($the_post['content']); ?>"></input>
				<input type="hidden" name="old_tags" value="<? echo $the_post['tags']; ?>"></input>
				<input type="hidden" name="old_comment_status" value="<? echo $the_post['comment_status']; ?>"></input>
				<input type="hidden" name="old_edited_by" value="<? echo $the_post['edited_by']; ?>"></input>
				<input type="hidden" name="old_edited_date" value="<? echo $the_post['edited_date']; ?>"></input>
				<input type="hidden" name="old_date" value="<? echo $the_post['posted_date_raw']; ?>"></input>
				<input type="hidden" name="old_permalink_name" value="<? echo $the_post['permalink_name']; ?>"></input>
				<input type="hidden" name="old_menu_name" value="<? echo $the_post['menu_name']; ?>"></input>
				<input type="hidden" name="old_lat" value="<? echo $the_post['lat']; ?>"></input>
				<input type="hidden" name="old_lon" value="<? echo $the_post['lon']; ?>"></input>

				<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit"></input><input type="submit" name="continue" onclick="document.editor.action = 'blog.php?edit=<? echo drizzo_post_id(); ?>&action=continue'" value="Save &amp; Continue Editing" class="submit"></input><input type="submit" name="submit" onclick="document.editor.action = 'blog.php?action=publish'" value="Publish" class="submit"></input></p>

			</div>
			
			<div id="options">
				
				<? drizzo_post_authors_dropdown(); ?>
				
				<? drizzo_types_dropdown(); ?>

				<? drizzo_post_statuses_dropdown(); ?>
				
				<? drizzo_post_comment_statuses_dropdown(); ?>
				
				<? drizzo_post_permalink_name_input(); ?>
				
				<? drizzo_edit_status(); ?>

			</div>

			</form>
<?
		}
	} // End $edit || $action == continue_new
	
	if($action == "new") {
?>
			<form id="editor_form" name="editor" method="post" action="">

			<div id="editor">
	
				<label for="title">Title</label><input name="title" type="text" value=""></input><br />

				<label for="content">Content</label><textarea name="content"></textarea><br />

				<label for="tags">Tags<br /><small>(Separate by commas)</small></label><textarea name="tags" type="text" id="tags_input"></textarea><br />
	
				<label for="recent_tags">Recently Used Tags</label><p id="recent_tags"><? echo drizzo_recent_tags(); ?></p>

				<div id="new_date" style="display:none;">
					<label for="date">Date</label>	<input name="date_month" class="date" type="text" value="<? echo date('m'); ?>"></input>
													<input name="date_day" class="date" type="text" value="<? echo date('d'); ?>"></input>
													<input name="date_year" class="date" type="text" value="<? echo date('Y'); ?>"></input>, 
													<input name="date_hour" class="date" type="text" value="<? echo date('H'); ?>"></input>:
													<input name="date_min" class="date" type="text" value="<? echo date('i'); ?>"></input>:
													<input name="date_secs" class="date" type="text" value="<? echo date('s'); ?>"></input>
													<br />
				</div>

				<p></p>
				
				<div id="new_date_toggle"><a href="javascript:void(0);" onclick="Element.hide(this);new Effect.Appear('new_date')">Want to Post to the Future?</a></div>

				<input type="hidden" name="start_time" value="<? echo time(); ?>"></input>

				<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit"></input><input type="submit" name="continue" onclick="document.editor.action = 'blog.php?action=continue_new'" value="Save Draft" class="submit"></input><input type="submit" name="submit" onclick="document.editor.action = 'blog.php?action=publish'" value="Publish" class="submit"></input></p>

			</div>

			<div id="options">
	
				<? drizzo_post_authors_dropdown(session_user_id); ?>
	
				<? drizzo_types_dropdown(); ?>

				<? drizzo_post_statuses_dropdown(); ?>
	
				<? drizzo_post_comment_statuses_dropdown(); ?>
	
				<? drizzo_post_permalink_name_input(); ?>
	
			</div>

			</form>
<?
	} // End $action == new
?>
			<br class="clear" />

		</div>
<?
	if($edit) {
?>		
		<div id="preview_wrap">
			
			<div id="preview">
			
				<h2>Post Preview</h2>
			
				<? if(strlen($the_post['content'])>0) { echo drizzo_texturize($the_post['content']); } else { echo "<p>Save the post to view this preview.</p>"; } ?>

			</div>

		</div>
<?
	}
	include_once('footer.php');
?>